<template>
    <el-dialog v-model="editData.visible" :title="editData.title" center width="30%" top="10vh" :close-on-click-modal="false">
        <div class="dialog-content">
            <el-form :model="editData.form" label-width="auto" style="width:100%" ref="formRef" :rules="editData.rules" disabled>
                <el-row :gutter="50">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="类型" prop="changeValue">
                            <el-radio-group v-model="editData.form.changeType" :disabled="editData.type==='update'">
                                <el-radio :label="1">模拟比赛获取</el-radio>
                                <el-radio :label="2">后台添加</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" v-if="editData.form.changeType===1">
                        <el-form-item label="比赛记录" prop="scoreRecordId">
                            <el-input v-model="editData.form.scoreRecordId" placeholder="请输入比赛记录ID" clearable style="width:60%" :min="0"/>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" v-if="editData.form.changeType===2">
                        <el-form-item label="用户积分" prop="changeValue">
                            <el-input-number v-model="editData.form.changeValue" placeholder="请输入用户积分" clearable style="width:60%" :min="0"/>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="变动理由" prop="changeReason">
                            <el-input type="textarea" rows="4" v-model="editData.form.changeReason" placeholder="请输入变动理由" clearable/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="editData.visible = false">取消</el-button>
                <el-button type="primary" @click="submitForm">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>


<script setup>
import { nextTick, reactive, ref, useTemplateRef } from 'vue'
import { ElMessage } from 'element-plus'
import pointsApi from '@/api/model/points.js'

const props = defineProps({
    userId: {
        type: [Number, String]
    }
})
const emits = defineEmits(['update'])
defineExpose({ openDialog })

const formRef = useTemplateRef('formRef')
const editData = reactive({
    type: 'add',
    visible: false,
    form: {
        id: '',
        userId: '',
        changeValue: '',
        changeReason: '',
        changeType: 1,
        scoreRecordId: ''
    },
    // rules: {
    //   changeValue: [
    //     {required: true, message: '请输入用户积分', trigger: 'blur'},
    //   ],
    // }
})

/**
 * 打开弹框
 * @param type
 * @param item
 */
function openDialog (type, item) {
    editData.title = type === 'add' ? '新增用户积分' : '编辑用户积分'
    editData.visible = true
    editData.type = type
    editData.form.id = ''
    nextTick(() => {
        formRef.value.resetFields()
        if (type === 'update') {
            const { id, changeValue, changeReason, changeType, scoreRecordId } = item || {}
            editData.form.id = id
            editData.form.changeValue = changeValue
            editData.form.changeReason = changeReason
            editData.form.changeType = changeType
            editData.form.scoreRecordId = scoreRecordId
        }
    })
}

/**
 * 提交表单
 */
function submitForm () {
    formRef.value.validate(async valid => {
        if (!valid) return
        editData.type === 'add' ? addItem() : updateItem()
    })
}

/**
 *  新增操作
 */
async function addItem () {
    const { code } = await pointsApi.add.post({
        userId: props.userId,
        changeValue: editData.form.changeValue,
        changeReason: editData.form.changeReason,
        changeType: editData.form.changeType,
        scoreRecordId: editData.form.scoreRecordId,
    })

    if (code === 200) {
        ElMessage.success('新增成功')
        editData.visible = false
        emits('update')
    } else {
        ElMessage.error('新增失败')
    }
}

/**
 *  更新操作
 */
async function updateItem () {
    const { code } = await pointsApi.update.post({
        id: editData.form.id,
        userId: props.userId,
        changeValue: editData.form.changeValue,
        changeReason: editData.form.changeReason,
        changeType: editData.form.changeType,
        scoreRecordId: editData.form.scoreRecordId,
    })
    if (code === 200) {
        ElMessage.success('编辑成功')
        editData.visible = false
        emits('update')
    } else {
        ElMessage.error('编辑失败')
    }
}
</script>
